function [outConsumerKey, outConsumerSecret, ...
          outAccessToken, outAccessTokenSecret] = twitpref(opt) %#ok<INUSD>
%TWITPREF Save twitter access tokens
%   Opens a GUI for entering twitter access tokens. Note that the access
%   tokens are not masked when entering them. It stores the tokens in the
%   preferences. This is queried by the twit function when that function is
%   not supplied with the access tokens.
%
%   This function stores all the access information in plain text and is
%   not secure. Use this at your own risk.
%
%   See also twit.

%   Copyright 2008-2010 The MathWorks, Inc.
%   Author: Navan Ruthramoorthy

groupName = 'TwitterAuthentication';
prefName  = 'AccessTokens';

if nargin == 0
  FigureHandle = figure( ...
                    'Visible','off', ...
                    'Menubar','none', ...
                    'Toolbar','none', ...
                    'Position', [360,500,420,180], ...
                    'IntegerHandle', 'off', ...
                    'Color',    get(0, 'defaultuicontrolbackgroundcolor'), ...
                    'NumberTitle', 'off', ...
                    'Name', 'Twitter username and password');
  movegui(FigureHandle, 'center');
  set(FigureHandle,'Visible','on');
  uicontrol('Parent', FigureHandle, 'Style', 'Text', ...
            'String', 'Consumer key', ...
            'Units', 'Normalized', 'Position', [0 0.8 0.4 0.15], ...
            'FontSize', 12);
  hCK = uicontrol('Parent', FigureHandle, 'Style', 'edit', ...
              'Units', 'Normalized', 'Position', [0.4 0.8 0.5 0.15], ...
              'FontSize', 12, 'BackgroundColor', 'white',...
              'HorizontalAlignment', 'left');
  uicontrol('Parent', FigureHandle, 'Style', 'Text', ...
              'String', 'Consumer secret', ...
              'Units', 'Normalized', 'Position', [0 0.60 0.4 0.15], ...
              'FontSize', 12);
  hCS = uicontrol('Parent', FigureHandle, 'Style', 'edit', ...
            'Units', 'Normalized', 'Position', [0.4 0.60 .5 0.15], ...
            'FontSize', 12, 'BackgroundColor', 'white', ...
            'HorizontalAlignment', 'left');
  uicontrol('Parent', FigureHandle, 'Style', 'Text', ...
              'String', 'Access token', ...
              'Units', 'Normalized', 'Position', [0 0.40 0.4 0.15], ...
              'FontSize', 12);
  hAT = uicontrol('Parent', FigureHandle, 'Style', 'edit', ...
            'Units', 'Normalized', 'Position', [0.4 0.40 .5 0.15], ...
            'FontSize', 12, 'BackgroundColor', 'white', ...
            'HorizontalAlignment', 'left');
  uicontrol('Parent', FigureHandle, 'Style', 'Text', ...
              'String', 'Access token secret', ...
              'Units', 'Normalized', 'Position', [0 0.2 0.4 0.15], ...
              'FontSize', 12);
  hATS = uicontrol('Parent', FigureHandle, 'Style', 'edit', ...
            'Units', 'Normalized', 'Position', [0.4 0.2 .5 0.15], ...
            'FontSize', 12, 'BackgroundColor', 'white', ...
            'HorizontalAlignment', 'left');
          
  uicontrol('Parent', FigureHandle, 'Style', 'pushbutton', ...
            'String', 'Submit', 'Units', 'Normalized', ...
            'Position', [0.1 0 .4 0.18], 'FontSize', 12, ...
            'Callback', @submitCallback);
  uicontrol('Parent', FigureHandle, 'Style', 'pushbutton', ...
            'String', 'Cancel', 'Units', 'Normalized', ...
            'Position', [0.5 0 .4 0.18], 'FontSize', 12, ...
            'Callback', @cancelCallback);
  set(FigureHandle', 'CloseRequestFcn', @cancelCallback);
  uiwait(FigureHandle);
else
  if ispref(groupName, prefName)
    Credentials = getpref(groupName, prefName);
    outConsumerKey = Credentials.ConsumerKey;
    outConsumerSecret = Credentials.ConsumerSecret;
    outAccessToken = Credentials.AccessToken;
    outAccessTokenSecret = Credentials.AccessTokenSecret;
  else
    outConsumerKey = '';
    outConsumerSecret = '';
    outAccessToken = '';
    outAccessTokenSecret = '';
  end
end

    function submitCallback(h, evd) %#ok<INUSD,INUSD>
        consumerKey = get(hCK, 'String');
        consumerSecret = get(hCS, 'String');
        accessToken = get(hAT, 'String');
        accessTokenSecret = get(hATS, 'String');
        setpref(groupName, prefName, struct('ConsumerKey', consumerKey, ...
                                    'ConsumerSecret', consumerSecret, ...
                                    'AccessToken', accessToken, ...
                                    'AccessTokenSecret', accessTokenSecret));
        delete(FigureHandle);
    end
    function cancelCallback(h, evd) %#ok<INUSD,INUSD>
        delete(FigureHandle);
    end
end
